/* ============================================================================
* 功能描述 ：按键函数实现
* 作者       ：ggls
* 创建日期 ：2024.5.22
* ----------------------------------------------------------------------------
* 更新历史 ：
*	日期 　　- 修改者   : 更新内容描述
*
* ============================================================================
*/


#include "main.h"


bit key_sleep = 1;



u16 pei_time = 0; 
u16 Distribution_time = 0; 


u32 led_time = 0;
u32 keysleep_time = 0;


bit unit_flag = 0;


unsigned int  uiKeyABTimeCnt12 = 0; 	//一起按下按键AB去抖动延时计数器
unsigned int  uiKeyATimeCnt12 = 0; 	//按下按键A去抖动延时计数器
unsigned int  double_Atime = 0; 	//按下按键A去抖动延时计数器

char key_event = 0;

bit buzzer_on_off = 0;
bit buzzer_mute = 0;
bit buzzer_buttonA = 0;
bit double_f = 0;
char LED_flag = 0;
char keysleep_flag = 0;

char double_A = 0;



void Hly_Key_Init(void)
{
	/* 设置io功能 */
	GPIO_SET_MUX_MODE(P03CFG, GPIO_MUX_GPIO);		//设置P03为GPIO模式
	GPIO_ENABLE_INPUT(P0TRIS, GPIO_PIN_3);			//设置为输入模式
	GPIO_ENABLE_UP(P0UP, GPIO_PIN_3);				//开启下拉
	
	GPIO_SET_MUX_MODE(P01CFG, GPIO_MUX_GPIO);		//设置P01为GPIO模式
	GPIO_ENABLE_INPUT(P0TRIS, GPIO_PIN_1);			//设置为输入模式
	GPIO_ENABLE_UP(P0UP, GPIO_PIN_1);				//开启下拉


	/* 设置中断方式 */
	GPIO_SET_INT_MODE(P03EICFG, GPIO_INT_FALLING);	//设置为下降沿中断模式
	GPIO_EnableInt(GPIO0, GPIO_PIN_3_MSK);			//开启P03中断

	GPIO_SET_INT_MODE(P01EICFG, GPIO_INT_FALLING);	//设置为下降沿中断模式
	GPIO_EnableInt(GPIO0, GPIO_PIN_1_MSK);			//开启P01中断

//	P30 = 1;
	/*
	(3)设置中断优先级
	*/
	IRQ_SET_PRIORITY(IRQ_P0, IRQ_PRIORITY_HIGH);
	/*
	(4)开启总中断
	*/	
	IRQ_ALL_ENABLE();

	
}



void key_scan(void) 
{
	
	ms_counter++;
	if(ms_counter == 10000)
	{
		ms_counter = 0;

	}

	if( P03 == 0 && P01 == 0)
	{
		uiKeyABTimeCnt12++;
		if(uiKeyABTimeCnt12 >= 3000)  // 双键长按三秒
		{
			key_event = 2;
		}
	}
	else if( P01 == 0 && P03 == 1) // 单机A键
	{
		uiKeyATimeCnt12++;
		
		double_A  = 1;			// 单机A计时标志
		
		
		if(uiKeyATimeCnt12 >= 3000 && uiKeyATimeCnt12 < 6000)   // A键长按三秒
		{
			key_event = 3;
		}
		if(uiKeyATimeCnt12 >= 6000)				// A键长按六秒
		{
			uiKeyATimeCnt12 = 0;
			key_event = 4;
		}

		
		
	
		if(double_Atime < 300 && double_Atime > 1)		// 300 ms内双击
		{
			
			key_event = 6;				
			double_Atime = 0;
			double_f = 1;
			double_A  = 0;
			uiKeyATimeCnt12 = 0;
		}


	


	}
	else if( P01 == 1 && P03 == 0)
	{
		
	}
	else if( P01 == 1 && P03 == 1)
	{

		if(double_f == 1)
		{
			double_f = 0;
			double_A = 0;
			double_Atime = 0;

		}
		if(double_A == 1 )		// 单机A之后开始计时
		{
			double_Atime++;
		}

		if(double_Atime >= 300 )		// 超过300ms算作单机
		{
			
			if(uiKeyATimeCnt12 > 0 && uiKeyATimeCnt12 < 500)
			{
				uiKeyATimeCnt12 = 0;
				key_event = 5;
				double_Atime = 0;
				double_A  = 0;
				
			}
		}
		
		if(uiKeyABTimeCnt12 > 0 && uiKeyABTimeCnt12 < 500)
		{
			uiKeyABTimeCnt12 = 0;
			key_event = 1;
		}
	
	//	uiKeyATimeCnt12 = 0;
	//	uiKeyABTimeCnt12 = 0;
	}

	if(keysleep_flag == 1)
	{
		keysleep_time++;
		if(keysleep_time == 10000)
		{
			keysleep_flag = 0;
			keysleep_time = 0;
		}

	}
	if(LED_flag == 1)
	{
		led_time++;
		P00 = 1;
		if(led_time > 1000)
		{
			P00 = 0;
			led_time = 0;
			LED_flag = 0;
		}

	}


	if(peiwang_flag == 1)
	{
		pei_time++;
		if(pei_time >= 200)
		{
			P00 = ~P00;
			Distribution_time++;
			pei_time = 0;					
			
		}

		if(Distribution_time == 300)
		{
			P14 = 1;
			delayms(50);
			bl_out_Network_msg();
			delayms(50);
			P14 = 0;
			P00 = 0;
			Distribution_time = 0;
			peiwang_flag = 0;

		}
	}
}

void key_service(void)
{
	switch(key_event)
	{
		case 1:
			key_event = 0;
	
			break;
		case 2:
			P14 = 1;
			delayms(50);	
			bl_Packet_Network_msg();
			delayms(50);	
			peiwang_flag = 1;
			key_event = 0;
			break;
		case 3:
			key_event = 0;
			buzzer_mute = 1;
			_data2_tx_pack();
		//	buzzer_buttonA = 0; 
			break;
		case 4:						
			key_event = 0;
			buzzer_on_off = ~buzzer_on_off;
		_data_tx_pack();
			break;
		case 5:
			key_event = 0;
			
			break;
		case 6:
			key_event = 0;
			
			break;
		
	
	}
}


